/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.javainfo.dao.inf.ArticleCategoryDaoIf;
import com.javainfo.dao.inf.BaseDaoIf;
import com.javainfo.entity.Article;
import com.javainfo.entity.ArticleCategory;
import com.javainfo.exception.AppException;
import com.javainfo.logic.ArticleSubArticleComponent;
import com.javainfo.service.inf.CategoryServiceIf;
import com.javainfo.simpledto.ArticleCategoryDto;
import com.javainfo.simpledto.ArticleDto;
import com.javainfo.util.PropertyComparator;

public class CategoryService  extends BaseService<ArticleCategory> implements CategoryServiceIf{

	private ArticleCategoryDaoIf articleCategoryDao;
	
	@Override
	public BaseDaoIf<ArticleCategory> getBaseDao() {
		return articleCategoryDao;
	}

	public ArticleCategoryDaoIf getArticleCategoryDao() {
		return articleCategoryDao;
	}

	public void setArticleCategoryDao(ArticleCategoryDaoIf articleCategoryDao) {
		this.articleCategoryDao = articleCategoryDao;
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.CategoryServiceIf#getBaseCategoryList()
	 */
	public List<ArticleCategory> getBaseCategoryList() {
		List<ArticleCategory> allList = articleCategoryDao.getAll();
		List<ArticleCategory> result = new ArrayList<ArticleCategory>();
		for(ArticleCategory articleCategory : allList){
			if(articleCategory.getUpCategory()==null){
				result.add(articleCategory);
			}
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.CategoryServiceIf#getCategoryList()
	 */
	public List<ArticleCategoryDto> getCategoryList() {
		List<ArticleCategory> baseCategoryList = getBaseCategoryList();
		for (ArticleCategory _articleCategory : baseCategoryList) {
			if (_articleCategory.getSubCategorys() != null
					&& _articleCategory.getSubCategorys().size() > 0) {
				ArticleSubArticleComponent.setArticles(_articleCategory);
			}
		}
		List<ArticleCategoryDto> list = new ArrayList<ArticleCategoryDto>();
		for (ArticleCategory _articleCategory : baseCategoryList) {
			list.add(getConvertUtil().convertArticleCategoryToSimpleDto(_articleCategory));
		}
		return list;
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.CategoryServiceIf#getCategoryInfo()
	 */
	public ArticleCategoryDto getCategoryInfo(String categoryId) {
		ArticleCategory articleCategory = getById(Integer.valueOf(categoryId));
		ArticleSubArticleComponent.setArticles(articleCategory);
		ArticleCategoryDto articleCategoryDto = new ArticleCategoryDto();
		articleCategoryDto.setBaseCategoryFlug(articleCategory.getBaseCategoryFlug());
		articleCategoryDto.setId(articleCategory.getId());
		articleCategoryDto.setCategoryName(articleCategory.getCategoryName());
		articleCategoryDto.setArticleNo(articleCategory.getArticles().size());
		return articleCategoryDto;
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.CategoryServiceIf#getCategoryArticles(java.lang.String)
	 */
	public List<ArticleDto> getCategoryArticles(String categoryId) {
		ArticleCategory articleCategory = getById(Integer.valueOf(categoryId));
		if (articleCategory.getSubCategorys() != null
				&& articleCategory.getSubCategorys().size() > 0) {
			ArticleSubArticleComponent.setArticles(articleCategory);
		}
		ArrayList<Article> articleList = new ArrayList<Article>();
		articleList.addAll(articleCategory.getArticles());
		Collections.sort(articleList, new PropertyComparator(true,
				"getUpdateTime"));
		ArrayList<ArticleDto> articleDtoList = new ArrayList<ArticleDto>();
		for (Article article : articleList) {
			articleDtoList.add(getConvertUtil().convertArticleToSimpleDto(article));
		}
		return articleDtoList;
	}
	
	@Override
	public void delete(ArticleCategory obj) {
		obj = getById(obj.getId());
		if(obj.getArticles()!=null&&obj.getArticles().size()>0){
			throw new AppException("分类有关联的文章，不能删除!");
		}else{
			super.delete(obj);
		}
	}
}
